<?php
/**
 *	@name		商品拍卖
 *	@author		Jeffery
 *	@qq			1017756891
 *	@email		1017756891@qq.com
 *	@website	http://www.iweitao.net
**/

require_once "core.php";

class threadplugin_iweitao_paipai extends safe_weitao_plugin_iweitao_paipai{

	var $name = "商品拍卖";
	var $iconfile = '';
	var $buttontext = "竞拍商品";
	
	function newthread($fid) {
		//发布新的帖子，显示表单
		global $_G,$groups,$types,$mades;
		authenticate_paipai();
		$thread_type = $this->get_thread_type();
		if(in_array($_G['groupid'],$groups)) { //判断是否在允许的用户组内
			include template("iweitao_paipai:newthread");
			return $return;
		} else {
			showmessage("您所在的用户组不允许使用该功能");
		}
	}

	function newthread_submit($fid) {
		//提交数据，对数据进行过滤操作
		authenticate_paipai();
		$price = $this->_post('price',1);
		$add_price = $this->_post('add_price',1);
		$title = $this->_post('title');
		$data_start = $this->_post('data_start');
		$data_end = $this->_post('data_end');
		$data_start = strtotime($data_start);
		$data_end = strtotime($data_end);
		if ($data_end <= $data_start) {
			showmessage("结束时间不能小于开始时间");
		}
		$imgs = array();
		for($a = 1; $a <= 5; $a++) {
			if($this->_post('auctionaid_url'.$a) != '') {
				$imgs[$a] = "GETIMG";
			}
			
		}
		if(!in_array("GETIMG",$imgs)) {
			showmessage('必须要上传一张图片');
		}
		if(!is_numeric($price) || !is_numeric($add_price)) {
			showmessage('价格必须为数字'); //您的价格必须为数字
		}
		if(empty($title)) {
			showmessage("商品名称不能为空");
		}
	}

	function newthread_submit_end($fid, $tid) {
		//验证成功，保存数据
		global $_G;
		$price = $this->_post('price',1);
		$add_price = $this->_post('add_price',1);
		$title = $this->_post('title');
		$data_start = $this->_post('data_start');
		$data_end = $this->_post('data_end');
		$data_start = strtotime($data_start);
		$data_end = strtotime($data_end);
		$type = $this->_post('type');
		$type_made = $this->_post('type_made');
		$send = $this->_post('send');
		$thread_type = $this->_post('thread_type');
		for($a = 1; $a <= 5; $a++) {
			if($a == 1) {
				convertunusedattach($this->_post("auctionaid1"),$tid,1);
				$auctionaid_url1 = $this->_post('auctionaid_url'.$a);
			} else if($a == 2) {
				convertunusedattach($this->_post("auctionaid2"),$tid,1);
				$auctionaid_url2 = $this->_post('auctionaid_url'.$a);
			} else if($a == 3) {
				convertunusedattach($this->_post("auctionaid3"),$tid,1);
				$auctionaid_url3 = $this->_post('auctionaid_url'.$a);
			} else if($a == 4) {
				convertunusedattach($this->_post("auctionaid4"),$tid,1);
				$auctionaid_url4 = $this->_post('auctionaid_url'.$a);
			} else if($a == 5) {
				convertunusedattach($this->_post("auctionaid5"),$tid,1);
				$auctionaid_url5 = $this->_post('auctionaid_url'.$a);
			}
		}
		$values = "null,'$tid','".$_G['uid']."','".$_G['username']."','$type','$type_made','$title',";
		$values2 = "'$price','$add_price','$send',";
		$values3 = "'$auctionaid_url1','$auctionaid_url2','$auctionaid_url3','$auctionaid_url4','$auctionaid_url5','$data_start','$data_end','$thread_type','1'";
		$str = $values.$values2.$values3.",'0'";
		DB::query("INSERT INTO `".DB::table("paipai_goods")."` VALUES($str)");
		$message = chr(0).chr(0).chr(0).'iweitao_paipai';
		DB::query("UPDATE ".DB::table("forum_post")." SET message = '".$message."' WHERE pid = '$pid'");
	}

	function editpost($fid, $tid) {
		
	}

	function editpost_submit($fid, $tid) {
		
	}

	function editpost_submit_end($fid, $tid) {
		
	}

	function newreply_submit_end($fid, $tid) {

	}

	function viewthread($tid) { //查看贴子，调用数据模板
		global $_G;
		global $admingroup;
		authenticate_paipai();
		$this->check_goods_status($tid,'message',TRUE);
		$detail = $this->getorderdetail($tid);
		$array = $this->getgoodsdetail_by_tid($tid);
		$time = time();
		if($time > $array['fetch_data_end'] && $array['status'] == 1) {
			$end = "<button type='button' class='pn' id='checkout' ><strong>结算交易</strong></button>";
		}
		$thread_type = $this->get_thread_type($array['identify']);
		$datas['Y'] = date("Y",$fetch_data_end);
		$datas['M'] = date("m",$fetch_data_end);
		$datas['D'] = date("d",$fetch_data_end);
		$datas['H'] = date("H",$fetch_data_end);
		$datas['I'] = date("i",$fetch_data_end);
		include template("iweitao_paipai:viewthread");
		return $return;
	}
	
	function getorderdetail($tid) {
		$query = DB::query("SELECT * FROM `".DB::table("paipai_buy")."` WHERE `tid` = '$tid' ORDER BY `price` ASC");
		while($fetch = mysql_fetch_array($query)) {	
			$time = date('Y-m-d',$fetch['time']);
			$array[] = array(
				'orderid' => $fetch['orderid'],
				'uid' => $fetch['uid'],
				'gid' => $fetch['gid'],
				'tid' => $fetch['tid'],
				'username' => $fetch['username'],
				'price' => $fetch['price'],
				'time' => $time,
				'status' => $fetch['status'],
			);
		}
		return $array;
	}
	
	function getorderdetail_by_orderid($orderid) {
		$query = DB::query("SELECT * FROM `".DB::table("paipai_buy")."` WHERE `orderid` = '$orderid' ORDER BY `price` DESC");
		while($fetch = mysql_fetch_array($query)) {	
			$time = date('Y-m-d',$fetch['time']);
			$array = array(
				'orderid' => $fetch['orderid'],
				'uid' => $fetch['uid'],
				'gid' => $fetch['gid'],
				'tid' => $fetch['tid'],
				'username' => $fetch['username'],
				'price' => $fetch['price'],
				'time' => $time,
				'status' => $fetch['status'],
			);
		}
		return $array;
	}
	function getgoodsdetail_by_tid($tid) {
		$query = DB::query("SELECT * FROM `".DB::table("paipai_goods")."` WHERE `tid` = '$tid'");
		while($fetch = mysql_fetch_array($query)) {
			$time = time();
			if($fetch['data_end'] > $time) {
				$time = $fetch['data_end'] - $time;
				$time = $time/24/60/60;
				$time = ceil($time);
				$str = "距离交易结束还有&nbsp;".$time."&nbsp;天";
			} else if($fetch['data_start'] > $time){
				$time = $fetch['data_start'] - $time;
				$time = $time/24/60/60;
				$time = ceil($time);
				$str = "距离交易开始还有&nbsp;".$time."&nbsp;天";
			} else {
				$str = "交易已经结束，感谢您的关注";
			}
			$data_start = date('Y-m-d H:i:s',$fetch['data_start']);
			$data_end = date('Y-m-d H:i:s',$fetch['data_end']);
			$array = array(
				'gid' => $fetch['gid'],
				'tid' => $fetch['tid'],
				'uid' => $fetch['uid'],
				'username' => $fetch['username'],
				'type' => $fetch['type'],
				'made' => $fetch['made'],
				'title' => $fetch['title'],
				'price' => $fetch['price'],
				'add_price' => $fetch['add_price'],
				'send' => $fetch['send'],
				'focus' => $fetch['focus'],
				'focus2' => $fetch['focus2'],
				'focus3' => $fetch['focus3'],
				'focus4' => $fetch['focus4'],
				'focus5' => $fetch['focus5'],
				'data_start' => $data_start,
				'data_end' => $data_end,
				'identify' => $fetch['identify'],
				'fetch_data_start' => $fetch['data_start'],
				'fetch_data_end' => $fetch['data_end'],
				'status' => $fetch['status'],
				'str' => $str,
				'freeze' => $fetch['freeze'],
			);
		}
		return $array;
	}
	function getgoodsdetail($gid) {
		$query = DB::query("SELECT * FROM `".DB::table("paipai_goods")."` WHERE `gid` = '$gid'");
		while($fetch = mysql_fetch_array($query)) {
			$array = array(
				'gid' => $fetch['gid'],
				'tid' => $fetch['tid'],
				'uid' => $fetch['uid'],
				'username' => $fetch['username'],
				'type' => $fetch['type'],
				'made' => $fetch['made'],
				'title' => $fetch['title'],
				'price' => $fetch['price'],
				'add_price' => $fetch['add_price'],
				'send' => $fetch['send'],
				'focus' => $fetch['focus'],
				'focus2' => $fetch['focus2'],
				'focus3' => $fetch['focus3'],
				'focus4' => $fetch['focus4'],
				'focus5' => $fetch['focus5'],
				'data_start' => $fetch['data_start'],
				'data_end' => $fetch['data_end'],
				'identify' => $fetch['identify'],
				'status' => $fetch['status'],
				'freeze' => $fetch['freeze'],
			);
		}
		return $array;
	}
	
	function check_goods_status($tid,$type = 'message',$admincp = FALSE) {
		global $_G;
		global $admingroup;
		if($admincp) {
			if($_G['groupid'] == $admingroup) {
				return true;
			}
		}
	
		$query = DB::query("SELECT * FROM `".DB::table('paipai_goods')."` WHERE `freeze` = 1 AND `tid` = '".$tid."'");
		if(mysql_fetch_array($query)) {
			if($type == 'message') {
				showmessage("对不起，当前交易已被冻结");
			} else {
				return false;
			}
			
		} else {
			return true;
		}
	}
	
	function get_thread_type($FILENAME = FALSE) { //获取当前安装扩展的文件
		global $DISCUZ_PAIPAI_VERSION;
		if($FILENAME) {
			if($FILENAME == 'default' || $FILENAME == '') {
				return "默认模式";
			}
			if(file_exists("./source/plugin/iweitao_paipai/$FILENAME/index.php")){
				require_once "./source/plugin/iweitao_paipai/$FILENAME/index.php";
				return $pluginname;
			} else {
				showmessage("扩展模块不存在，请联系管理员");
			}
		} else {
			$files = array('js','template','image','.','..'); //定义公用目录
			$handler = opendir("./source/plugin/iweitao_paipai/");
			while (($filename = readdir($handler)) !== false) { 
				if(!in_array($filename,$files)) {
					if(!is_dir("./source/plugin/iweitao_paipai/".$filename)) {
						continue;
					}
					if(!file_exists("./source/plugin/iweitao_paipai/$filename/index.php")){
						showmessage("扩展模块不存在，请联系管理员");
					}
					require_once "./source/plugin/iweitao_paipai/$filename/index.php";
					if($type != 'THREAD') {
						continue;
					}
					if($version_need > $DISCUZ_PAIPAI_VERSION) { //判断当前版本是否支持改钩子
						continue;
					}
					$loop[$filename] = $pluginname;
				}
			}
			return $loop;
		}
	
	}
	
	function getapihook($hookname) {
		global $DISCUZ_PAIPAI_VERSION;
		if($hookname == '') {
			return false;
		}
		
		$files = array('js','template','image','.','..'); //定义公用目录
		$handler = opendir("./source/plugin/iweitao_paipai/");
		while (($filename = readdir($handler)) !== false) { 
			if(!in_array($filename,$files)) {
				if(!is_dir("./source/plugin/iweitao_paipai/".$filename)) {
					continue;
				}
				if(!file_exists("./source/plugin/iweitao_paipai/$filename/index.php")){
					showmessage("扩展模块不存在，请联系管理员");
				}
				require_once "./source/plugin/iweitao_paipai/$filename/index.php";
				if($type != 'API') { //判断钩子类型
					continue;
				}
				if($version_need > $DISCUZ_PAIPAI_VERSION) { //判断当前版本是否支持改钩子
					continue;
				}
				foreach($load_files as $k => $v) {
					if($k == $hookname) {
						$loadingfiles[] = $loadpath.$v;
					}
				}
			}
		}
		return $loadingfiles;
	}
}
?>